package com.cloudptt.api.product.operator;

import com.cloudptt.api.comm.CauseCont;
import com.cloudptt.api.product.func.SipOperator;
import com.cloudptt.api.product.log.Log;
import com.mcptt.config.local.SysConfig;
import com.mcptt.config.model.McIdInfo;
import com.mcptt.config.model.McpttLoginInfo;
import com.mcptt.http.operator.IHttpOpCb;
import com.mcptt.http.operator.sys.SysHttpAuthLoader;
import com.mcptt.http.operator.sys.SysHttpGetLoader;

import android.content.Context;
import android.os.RemoteException;

public class SysOperator extends SipOperator
{
	 private static final String THIS_FILE = "SysOperator";
	 
//	private Context ctx = null;
	private SysConfig config = null;

	public SysOperator(Context ctx,SysConfig config)
	{
		super(ctx);
		Log.d(THIS_FILE, "SysOperator ServerInfo = " + config.getServerInfo());
//		this.ctx = ctx;
		this.config = config;
		
//		int resolving = config.getVideoConfig().getVideoResolving();
//		this.setVideoAttr(resolving);
	}
	
	public int login()
	{
		Log.d(THIS_FILE, "login");
		McpttLoginInfo mcpttLoginInfo = config.getMcpttLoginInfo();

		McIdInfo userInfo = mcpttLoginInfo.getMcIdInfo();
		String num = userInfo.getMcId();
		String pwd = userInfo.getPwd();

		try 
		{
			//logout();
			login(num, pwd);
		} catch (RemoteException e) {
			// TODO Auto-generated catch block
			return CauseCont.OperateCheck_Sys_OperateException;
		}
		
		return CauseCont.OperateCheck_Suc;
	}

	public int login(String num,String pwd) throws RemoteException
	{
		Log.d(THIS_FILE, "login num = " + num + " pwd = ******");
		int result = checkFirstForLogin();
		if(CauseCont.OperateCheck_Suc != result)
		{
			Log.d(THIS_FILE, "login result = " + result);
			return result;
		}

		this.config.setUserInfoBeforLogin(num, pwd);
		String svrIp =  this.config.getServerInfo().getIp();
		int port = this.config.getServerInfo().getPort();
		String userId = this.config.getUserID();
		String regJson = this.config.getSipAuthJson();
		float txLevel = config.getTxLevel();
		float rxLevel = config.getRxLevel();
//		McpttLoginInfo mcpttLoginInfo = config.getMcpttLoginInfo();
//		Log.d(THIS_FILE, "login mcpttLoginInfo = " + mcpttLoginInfo);
		service.addAccount(svrIp,port,userId,pwd,regJson,txLevel,rxLevel);
		return CauseCont.OperateCheck_Suc;
	}
	
//	public void refreshAccount()
//	{
//		try {
//			service.refreshAccount();
//		} catch (RemoteException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}
//	}
	
	public void httpGet(IHttpOpCb cb)
	{
		Log.d(THIS_FILE, "httpGet");
		SysHttpGetLoader loader = new SysHttpGetLoader(config,cb);
		loader.doHttpGet();
	}
	
	public void httpAuth(IHttpOpCb cb)
	{
		Log.d(THIS_FILE, "httpAuth ServerInfo = " + config.getServerInfo());
		SysHttpAuthLoader loader = new SysHttpAuthLoader(config,cb);
		loader.doHttpAuth();
	}

//	public int logout(String userKey)
//	{
//		Log.d(THIS_FILE, "logout");
//		int result = checkFirstForLogin();
//		if(CauseCont.OperateCheck_Suc != result)
//		{
//			return result;
//		}
//
//		try
//		{
//			service.removeAllAccounts();
//		}
//		catch (Exception e)
//		{
//			e.printStackTrace();
//			return CauseCont.OperateCheck_Sys_OperateException;
//		}
//		return CauseCont.OperateCheck_Suc;
//	}
	
	public int logout()
	{
		Log.d(THIS_FILE, "logout");
		int result = checkFirstForLogin();
		if(CauseCont.OperateCheck_Suc != result)
		{
			return result;
		}

		try 
		{
			service.removeAllAccounts();
		} 
		catch (Exception e) 
		{
			e.printStackTrace();
			return CauseCont.OperateCheck_Sys_OperateException;
		}
		return CauseCont.OperateCheck_Suc;
	}

	public int setCallGNum(String allCallGNum,String curGroupNum,String sosGroupNum,String selfNum)
	{
		Log.d(THIS_FILE,"setCallGNum allCallGNum = " + allCallGNum + " curGroupNum = " + curGroupNum  + " sosGroupNum = " + sosGroupNum  + " selfNum = " + selfNum);
		Log.d(THIS_FILE, "logout");
		int result = checkFirstForLogin();
		if(CauseCont.OperateCheck_Suc != result)
		{
			return result;
		}

		try
		{
			service.setCallGNum(allCallGNum,curGroupNum,sosGroupNum,selfNum);
		}
		catch (Exception e)
		{
			e.printStackTrace();
			return CauseCont.OperateCheck_Sys_OperateException;
		}
		return CauseCont.OperateCheck_Suc;
	}
	
	public int setVideoAttr(final int videoResolving)
	{
		Log.d(THIS_FILE, "setVideoAttr videoResolving = " + videoResolving);
		int result = checkFirstForLogin();
		if(CauseCont.OperateCheck_Suc != result)
		{
			return result;
		}

		try 
		{
			service.setVideoAttr(videoResolving);
		} 
		catch (Exception e) 
		{
			e.printStackTrace();
			return CauseCont.OperateCheck_Sys_OperateException;
		}
		return CauseCont.OperateCheck_Suc;
	}
	
//	public int addBuddy(int mcType,String mcId,String requestLineUri,String xCapStr)
//	{
//		Log.d(THIS_FILE, "addBuddy mcId = " + mcId + " requestLineUri = " + requestLineUri + " xCapStr = " + xCapStr);
//		int result = checkFirstForLogin();
//		if(CauseCont.OperateCheck_Suc != result)
//		{
//			return result;
//		}
//
//		try 
//		{
//			service.addBuddy(mcType,mcId,requestLineUri,xCapStr);
//		} 
//		catch (Exception e) 
//		{
//			e.printStackTrace();
//			return CauseCont.OperateCheck_Sys_OperateException;
//		}
//		return CauseCont.OperateCheck_Suc;
//	}
	
//	public int delAccount()
//	{
//		Log.d(THIS_FILE, "delAccount");
//		int result = checkFirstForLogin();
//		if(CauseCont.OperateCheck_Suc != result)
//		{
//			return result;
//		}
//
//		try
//		{
//			service.delCurAccount();
//		}
//		catch (Exception e)
//		{
//			e.printStackTrace();
//			return CauseCont.OperateCheck_Sys_OperateException;
//		}
//		return CauseCont.OperateCheck_Suc;
//	}
//
//	public int startPjsip()
//	{
//		Log.d(THIS_FILE, "startPjsip");
//		int result = checkFirstForLogin();
//		if(CauseCont.OperateCheck_Suc != result)
//		{
//			return result;
//		}
//
//		try
//		{
//			service.sipStart();
//		}
//		catch (Exception e)
//		{
//			e.printStackTrace();
//			return CauseCont.OperateCheck_Sys_OperateException;
//		}
//		return CauseCont.OperateCheck_Suc;
//	}
//
//	public int stopPjsip()
//	{
//		Log.d(THIS_FILE, "stopPjsip");
//		int result = checkFirstForLogin();
//		if(CauseCont.OperateCheck_Suc != result)
//		{
//			return result;
//		}
//
//		try
//		{
//			service.sipStop();
//		}
//		catch (Exception e)
//		{
//			e.printStackTrace();
//			return CauseCont.OperateCheck_Sys_OperateException;
//		}
//		return CauseCont.OperateCheck_Suc;
//	}

//	public int sendChPwdSysMsg(String oldPwd,String pwd) throws RemoteException
//	{
//		Log.d(THIS_FILE, "sendChPwdSysMsg oldPwd = " + oldPwd + " pwd = " + pwd);
//		String msgStr = "";//sysMsgCreator.modifyPasswordMsg(oldPwd, pwd);
//		return sendSysMsg(msgStr);
//	}
//
//	public int sendQryNewUserMsg(String num) throws RemoteException
//	{
//		Log.d(THIS_FILE, "sendQryNewUserMsg num = " + num);
//		String msgStr = "";//sysMsgCreator.qryNewUserMsg(num);
//		return sendSysMsg(msgStr);
//	}
//
//	private int sendSysMsg(String msgStr)  throws RemoteException
//	{
//		Log.d(THIS_FILE, "sendSysMsg msgStr = " + msgStr);
//		int result = checkFirst();
//		if(CauseCont.OperateCheck_Suc != result)
//		{
//			return result;
//		}
//		service.sendSystemMessage(msgStr);
//		return CauseCont.OperateCheck_Suc;
//	}

//	public int startHandShakeTimer()
//	{
//		Log.d(THIS_FILE, "startHandShakeTimer");
//		int result = checkFirst();
//		if(CauseCont.OperateCheck_Suc != result)
//		{
//			return result;
//		}
//
//		try {
//			service.startHandShakeTimer("");
//		} 
//		catch (Exception e) 
//		{
//			e.printStackTrace();
//			return CauseCont.OperateCheck_Sys_OperateException;
//		}
//		return CauseCont.OperateCheck_Suc;
//	}
//
//	public int stoptHandShakeTimer()
//	{
//		Log.d(THIS_FILE, "stoptHandShakeTimer");
//		int result = checkFirst();
//		if(CauseCont.OperateCheck_Suc != result)
//		{
//			return result;
//		}
//
//		try {
//			service.stopHandShakeTimer();
//		} 
//		catch (Exception e) 
//		{
//			e.printStackTrace();
//			return CauseCont.OperateCheck_Sys_OperateException;
//		}
//		return CauseCont.OperateCheck_Suc;
//	}
}
